const router = require('koa-router')();
const DB = require('../../module/db.js');
const tools = require('../../module/tools.js');

router.get('/', async (ctx) => {
	
	let result = await DB.findOne('setting', {});
	
    await ctx.render('admin/setting/list', {
    	list: result[0]
    });
});

router.post('/', tools.multer().single('site_logo'), async (ctx) => {
	
	let id = ctx.req.body.id;
	let site_logo = ctx.req.file ? ctx.req.file.path.replace('static\\', "") : ''; // 返回文件名
	
	let json = {
		site_name: ctx.req.body.site_name.trim(),
		keywords: ctx.req.body.keywords.trim(),
		description: ctx.req.body.description.trim(),
		icp: ctx.req.body.icp.trim(),
		site_qq: ctx.req.body.site_qq.trim(),
		status: parseInt(ctx.req.body.status, 0) || 0
	};
	
	if(site_logo){
		json['site_logo'] = site_logo;
	}
	
	console.log(id);
	
	let result = await DB.updateOne('setting', {
		_id: DB.getObjectID(id)
	}, json);
	
	ctx.redirect(ctx.state.__HOST__ + '/admin/setting');
});



module.exports = router.routes();